package spring.angularjs.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import spring.angularjs.domain.Todo;
import spring.angularjs.services.Services;

@Controller
@RequestMapping(value="/todo")
public class TodoController {
	
	@Autowired
	private Services services;

	@RequestMapping(value="/start")
	public String getTodoPartialPage(){
		return "todo/layout";
	}
	
	@RequestMapping(value = "/all.json", method=RequestMethod.GET,headers="Accept=application/json")
	@ResponseBody
	public List<Todo> getAllTodoUser(){
		return services.getListTodo();
	}
	
	@RequestMapping(value = "/add/{name}", method=RequestMethod.POST,headers="Accept=application/json")
	@ResponseBody
	public void addNewTodoUser(@PathVariable("name") String name){
		Todo todo = new Todo(name);
		services.addTodo(todo);
	}
	
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE, headers="Accept=application/json")
	@ResponseBody
	public void deleteTodoUser(@PathVariable("id") int id){
		Todo todo = services.getTodo(id);
		services.deleteTodo(todo);
	}
	
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.POST, headers="Accept=application/json")
	@ResponseBody
	public Todo getUserTodo(@PathVariable("id") int id){
		Todo todo = services.getTodo(id);
		return todo;
	}
	
	@RequestMapping(value = "/update/{id}/{name}", method = RequestMethod.PUT, headers="Accept=application/json")
	@ResponseBody
	public void updateUserTodo(@PathVariable("id") int id, @PathVariable("name") String name){
		Todo todo = services.getTodo(id);
		todo.setName(name);
		services.updateTodo(todo);
	}
	
	@RequestMapping(value = "/deleteAll", method = RequestMethod.DELETE, headers = "Accept=application/json")
	@ResponseBody
	public void deleteAllTodo(){
		services.deleteAllTodo();
	}
}
